package com.cinema.servlet;

import java.io.IOException;
import java.security.MessageDigest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cinema.Bean.Admin;
import com.cinema.Bean.User;
import com.cinema.dao.IAdminDao;
import com.cinema.dao.IUserDao;
import com.cinema.dao.imp.AdminDaoMysql;
import com.cinema.dao.imp.UserDaoMysql;

public class loginServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	IUserDao  iUserDao=new UserDaoMysql();
	
	IAdminDao iAdminDao = new AdminDaoMysql();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html,charset=utf-8");
		String UserId = req.getParameter("userId"); // 用户账号
		String Upwd = getMD5(req.getParameter("userPwd")); 
		String identity = req.getParameter("user"); // 判断是用户或管理员
		String user = "user";
		String admin = "admin";
		if (UserId!=null || Upwd!=null || identity!=null) {
			if(user.equals(identity)) { // 用户登录
				User u=iUserDao.SelectById(UserId);
				
				if(u!=null && u.getUserPwd().equals(Upwd)){
					//保存登录信息
					req.getSession().setAttribute("loginUser", u);
					String path = req.getContextPath();
					String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";
					//重定向到用户主页面
					resp.sendRedirect(basePath+"indexServlet");
				}else{
					resp.getWriter().append("<script>alert('There is something wrong with your password !!!');window.location.href='login.jsp';</script>");
				}
				
			}else if(admin.equals(identity)) { // 管理员登录
				
				Admin a = iAdminDao.SelectById(UserId);
				if(a!=null && a.getAdminPwd().equals(Upwd)){
					//保存登录信息
					req.getSession().setAttribute("loginManage", a);
					//重定向到用户主页面
					resp.sendRedirect("indexManage.jsp");
				}else{
					resp.getWriter().append("<script>alert('There is something wrong with your password !!!');window.location.href='login.jsp';</script>");
				}
			}
			
		}
	}
	
	/** 
     * 对密码md5加密(大写+数字) 
     * @param str 传入要加密的字符串 
     * @return  MD5加密后的字符串 
     */  
    public static String getMD5(String s) {  
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};         
  
        try {  
            byte[] btInput = s.getBytes();  
            // 获得MD5摘要算法的 MessageDigest 对象  
            MessageDigest mdInst = MessageDigest.getInstance("MD5");  
            // 使用指定的字节更新摘要  
            mdInst.update(btInput);  
            // 获得密文  
            byte[] md = mdInst.digest();  
            // 把密文转换成十六进制的字符串形式  
            int j = md.length;  
            char str[] = new char[j * 2];  
            int k = 0;  
            for (int i = 0; i < j; i++) {  
                byte byte0 = md[i];  
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];  
                str[k++] = hexDigits[byte0 & 0xf];  
            }  
            return new String(str);  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    }
}
